<?php

class Admin_Form_OfferTemplate_Add extends My_FormAdmin {

    protected $offerTemplateId;

    public function __construct($offerTemplateId, $options = null) {
        $this->offerTemplateId = $offerTemplateId;
        parent::__construct($options);
    }

    public function init() {
        parent::init();
        $this->setMethod('post');
        $this->setAction($this->getView()->url(array('controller' => 'offer-template', 'action' => 'save'), 'admin', true));

        $this->setTitle($this->translate('XXX_ADMIN_EVENT_ADD_FORM_TITLE'));


        $daoOfferTemplate = new Application_Model_DAO_OfferTemplate();
        $tpl = $daoOfferTemplate->getTemplate($this->offerTemplateId);
        if (empty($tpl)) {
            $attribs = array();
        } else {
            $attribs = $tpl['attributes'];
        }

        $inputTypes = array('text' => 'Text', 'number' => 'Wartość liczbowa', 'textarea' => 'Textarea', 'checkbox' => 'Checkbox', 'select' => 'Select');
        $cnt = count($attribs) + 5;
        for ($i = 0; $i < $cnt; $i++) {

            $content = new My_Form_Element_Content('cnt_' . $i);
            $content->setValue('Atrybut ' . ($i + 1));
            $this->addElement($content);

            $prefix = 'e' . $i . '_';
            $display = $this->createElement('text', $prefix . 'display_pl')
                    ->setLabel('Nazwa atrybutu');
            $required = $this->createElement('checkbox', $prefix . 'required')
                    ->setLabel('Wymagany');
            $inputType = $this->createElement('select', $prefix . 'input_type', array(
                        'multiOptions' => $inputTypes,
                    ))
                    ->setLabel('Typ elementu');
            $size = $this->createElement('select', $prefix . 'size', array(
                        'multiOptions' => array('small' => 'Jedna kolumna', 'big' => 'Dwie kolumny')
                    ))
                    ->setLabel('Rozmiar');
            $inputParams = $this->createElement('text', $prefix . 'input_params')
                    ->setLabel('Dodatkowe parametry');

            $elements = array(
                $display, $inputType, $inputParams, $size, $required
            );
            $this->addElements($elements);
        }

        $attrKeys = array('display_pl', 'required', 'input_type', 'size', 'input_params');
        foreach ($attribs as $i => $attr) {
            $prefix = 'e' . $i . '_';
            foreach ($attrKeys as $key) {
                $ename = $prefix . $key;
                $this->getElement($ename)->setValue($attr[$key]);
            }
            $val = $attr['input_params'];
            if (!empty($val)) {
                try {
                    $d = Zend_Json::decode($val);
                    if(is_array($d)) {
                        $tmp = array();
                        foreach ($d as $d0) {
                            $tmp[] = str_replace(',', ',,', $d0);
                        }
                        $d = implode(', ', $tmp);
                        $this->getElement($prefix.'input_params')->setValue($d);
                    }
                } catch (Exception $ex) {
                    
                }
            }
        }

        $this->addSubmitElement();

        $this->addElement('hidden', 'id');
    }

    public function getValues($suppressArrayNotation = false) {
        $data = parent::getValues($suppressArrayNotation);
        $attribs = array();
        $attrKeys = array('display_pl', 'required', 'input_type', 'size', 'input_params');

        for ($i = 0;; $i++) {
            $prefix = 'e' . $i . '_';
            if ($this->getElement($prefix . 'input_type')) {
                $val = trim($this->getElement($prefix . 'display_pl')->getValue());
                if (empty($val)) {
                    continue;
                }
                $attr = array();
                foreach ($attrKeys as $key) {
                    $attr[$key] = $this->getElement($prefix . $key)->getValue();
                }
                // process additional params
                $val = trim($attr['input_params']);
                if (empty($val)) {
                    $val = null;
                } else {
                    try {
                        $tmp = Zend_Json::decode($val);
                        if($tmp == null) {
                            throw new My_Exception('JSON ERROR');
                        }
                    } catch (Exception $ex) {
                        $cst = '-' . uniqid() . '-';
                        $val = str_replace(',,', $cst, $val);
                        $vals = explode(',', $val);
                        $tmp = array();
                        foreach ($vals as $v) {
                            $v = str_replace($cst, ',', $v);
                            $tmp[] = trim($v);
                        }
                        $val = Zend_Json::encode($tmp);
                    }
                }
                $attr['input_params'] = $val;
                $attribs[] = $attr;
            } else {
                break;
            }
        }
        $data['attributes'] = $attribs;
        return $data;
    }

}

